package script.db

databaseChangeLog(logicalFilePath: 'script/db/hpfm_cusz_model_relation.groovy') {
    changeSet(author: "hzero@hand-china.com", id: "2022-07-01-hpfm_cusz_model_relation") {
        def weight = 1
        if (helper.isSqlServer()) {
            weight = 2
        } else if (helper.isOracle()) {
            weight = 3
        }
        if (helper.dbType().isSupportSequence()) {
            createSequence(sequenceName: 'hpfm_cusz_model_relation_s', startValue: "1")
        }
        createTable(tableName: "hpfm_cusz_model_relation", remarks: "") {
            column(name: "id", type: "bigint", autoIncrement: true, remarks: "表ID，主键，供其他表做外键") { constraints(primaryKey: true) }
            column(name: "relation_type", type: "varchar(" + 30 * weight + ")", remarks: "关系类型。包括ONE_TO_ONE，ONE_TO_MANY") { constraints(nullable: "false") }
            column(name: "master_model_code", type: "varchar(" + 60 * weight + ")", remarks: "主模型编码") { constraints(nullable: "false") }
            column(name: "slave_model_code", type: "varchar(" + 60 * weight + ")", remarks: "关联模型编码") { constraints(nullable: "false") }
            column(name: "tenant_id", type: "bigint", defaultValue: "0", remarks: "租户ID") { constraints(nullable: "false") }
            column(name: "object_version_number", type: "bigint", defaultValue: "1", remarks: "行版本号，用来处理锁") { constraints(nullable: "false") }
            column(name: "created_by", type: "bigint", defaultValue: "-1", remarks: "") { constraints(nullable: "false") }
            column(name: "creation_date", type: "datetime", defaultValueComputed: "CURRENT_TIMESTAMP", remarks: "") { constraints(nullable: "false") }
            column(name: "last_updated_by", type: "bigint", defaultValue: "-1", remarks: "") { constraints(nullable: "false") }
            column(name: "last_update_date", type: "datetime", defaultValueComputed: "CURRENT_TIMESTAMP", remarks: "") { constraints(nullable: "false") }
        }

        addUniqueConstraint(columnNames: "relation_type,master_model_code,slave_model_code,tenant_id", tableName: "hpfm_cusz_model_relation", constraintName: "hpfm_cusz_model_relation_u1")
    }

    changeSet(author: "hzero@hand-china.com", id: "2022-10-18-hpfm_cusz_model_relation") {
        dropUniqueConstraint(tableName: 'hpfm_cusz_model_relation', constraintName: 'hpfm_cusz_model_relation_u1')
    }

    changeSet(author: "hzero@hand-china.com", id: "2022-10-18-hpfm_cusz_model_relation-1") {
        createIndex(tableName: "hpfm_cusz_model_relation", indexName: "hpfm_cusz_model_relation_n1") {
            column(name: "relation_type")
            column(name: "master_model_code")
            column(name: "slave_model_code")
            column(name: "tenant_id")
        }
    }
}